package hu.mapro.gwtui.client.login;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import hu.mapro.gwtui.client.Action;
import hu.mapro.gwtui.client.Menu;
import hu.mapro.gwtui.client.MenuGroup;
import hu.mapro.gwtui.client.MenuItem;
import hu.mapro.gwtui.client.Visibility;
import hu.mapro.gwtui.client.VisibilityUtils;

@Singleton
public class LoginUiBuilder {

	Menu ui;
	LoginService loginService;
	
	@Inject
	public LoginUiBuilder(Menu ui, LoginService loginService) {
		super();
		this.ui = ui;
		this.loginService = loginService;
	}

	public void build() {
		MenuGroup userGroup = ui.addMenuGroup();
		userGroup.setText("User");
		MenuItem loginItem = userGroup.addMenuItem();
		loginItem.setText("Login");
		VisibilityUtils.addVisibility(LoginUtils.whenLoggedOut(loginService), loginItem);
		loginItem.setAction(new Action() {
			@Override
			public void perform() {
				loginService.doLogin();
			}
		});
		
		MenuItem logoutItem = userGroup.addMenuItem();
		logoutItem.setText("Logout");
		VisibilityUtils.addVisibility(LoginUtils.whenLoggedIn(loginService), logoutItem);
		logoutItem.setAction(new Action() {
			@Override
			public void perform() {
				loginService.doLogout();
			}
		});
	}
	

}
